<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['game_id']){
		$params['game_id']=intval($_RQ['game_id']);
		$return['params']['game_id']=$_RQ['game_id'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'createtime desc',
		
	);
	$list=$this->get_list('sao_log',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']),array('from_os','nickname','avatar','mobile','sex','pcd'));
			$item['mobile']=$this->get_column('member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']),'mobile');
			$item['mobile']=$item['mobile']?:$item['member']['mobile'];
			$game=$this->pdo_get('game',array('id'=>$item['game_id']));
			$upvideo=$this->model('model')->check_upvideo($game,$item['mid']);
			$item['updatatime']=$upvideo['updatatime'];
			if($upvideo['id']){
				$item['isupvideo']=1;
			}else{
				$item['isupvideo']=0;
			}
			$item['videodata']=$upvideo;
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		//'export'=>'export',
		'tabs'=>array(
			array('op'=>'list','title'=>'扫码记录'),
		),
		'list'=>array('data'=>'list','tip'=>'<red>[温馨提示]：视频播放数据存在一步差异，点击《更新》可以同步数据！如果提示“access_token”失效，说明需要客户重新授权（用户访问一下活动，将自动重新授权！）！','delall'=>false,'list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'mobile','title'=>'手机号','type'=>'string'),
			array('name'=>'game_id','title'=>'所属活动','type'=>'select','options'=>'games'),
			array('name'=>'member','title'=>'所属商户','type'=>'member'),
			array('type'=>'string','title'=>'平均播放时长','name'=>'videodata.avg_play_duration'),
			array('type'=>'string','title'=>'评论数','name'=>'videodata.total_comment'),
			array('type'=>'string','title'=>'点赞数','name'=>'videodata.total_like'),
			array('type'=>'string','title'=>'播放数','name'=>'videodata.total_play'),
			array('type'=>'string','title'=>'分享数','name'=>'videodata.total_share'),
			array('name'=>'updatatime','title'=>'同步时间','type'=>'time'),
			array('name'=>'createtime','title'=>'参与时间','type'=>'time'),
			array('type'=>'radio','options'=>'isupvideo','title'=>'视频发送状态','name'=>'isupvideo'),
		),'action'=>array(
			array('type'=>'modal','op'=>'videodata','title'=>'视频数据'),
			//array('type'=>'post','op'=>'upvideodata','title'=>'更新'),
		)),
		'search'=>array(
			array('name'=>'game_id','width'=>15,'title'=>'所属活动','type'=>'select','options'=>'games'),
			array('name'=>'createtime','width'=>15,'title'=>'创建时间','type'=>'time_range'),
		),
	);
	$return['isupvideo']=array('发送视频失败','发送成功');
	$return['games']=$this->pdo_getall('game',array('uniacid'=>$_SESSION['uniacid']));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='upvideodata'){
	$id=intval($_RQ['id']);
	$item=$this->pdo_get('sao_log',array('id'=>$id),'id');
	$game=$this->pdo_get('game',array('id'=>$item['game_id']));
	$upvideo=$this->model('model')->check_upvideo($game,$item['mid']);
	$res=$this->model('model')->upvideodata($upvideo);
	if($res['error']){
		exi($res['message'],'error');
	}
	exi('同步数据成功！');
}
if($_RQ['op']=='videodata'){
	$id=intval($_RQ['id']);
	$item=$this->pdo_get('sao_log',array('id'=>$id),'id');
	$game=$this->pdo_get('game',array('id'=>$item['game_id']));
	$upvideo=$this->model('model')->check_upvideo($game,$item['mid']);
	$item=$upvideo['data']?json_decode($upvideo['data'],true):array();
	if($upvideo['id']){
		$item['isupvideo']=1;
	}else{
		$item['isupvideo']=0;
	}
	$item['paytime']=0;
	$htmls=array(
		'type'=>'look',
		'desc'=>'视频数据',
		'look'=>array(
			array('type'=>'radio','options'=>'isupvideo','title'=>'视频发送状态','name'=>'isupvideo','read'=>true),
			array('type'=>'string','title'=>'平均播放时长','name'=>'avg_play_duration','read'=>true),
			array('type'=>'string','title'=>'评论数','name'=>'total_comment','read'=>true),
			array('type'=>'string','title'=>'点赞数','name'=>'total_like','read'=>true),
			array('type'=>'string','title'=>'播放数','name'=>'total_play','read'=>true),
			array('type'=>'string','title'=>'分享数','name'=>'total_share','read'=>true),
		)
	);
	$return['isupvideo']=array('发送视频失败','发送成功');
	$return['item']=$item;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请选择记录！！','error');
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->pdo_delete('sao_log',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>